package com.homework.action;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.homework.bean.CourseBean;
import com.homework.bean.HomeworkBean;
import com.homework.bean.PageInfoBean;
import com.homework.bean.StudentHomeworkListBean;
import com.homework.bean.UserBean;
import com.homework.service.HomeworkService;

public class HomeworkQueryAction {

	private HomeworkService homeworkService;
	private String realname;
	private String batch;
	private int courseId;
	private int intPage;
	
	public HomeworkQueryAction() {
		// TODO Auto-generated constructor stub
		homeworkService = new HomeworkService();
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public String getBatch() {
		return batch;
	}

	public void setBatch(String batch) {
		this.batch = batch;
	}

	public int getCourseId() {
		return courseId;
	}

	public void setCourseId(int courseId) {
		this.courseId = courseId;
	}

	public int getIntPage() {
		return intPage;
	}

	public void setIntPage(int intPage) {
		this.intPage = intPage;
	}

	// 作业查询_教师(根据realname和courseId查找)
	public String homeworkQuery_teacher() throws SQLException{
		HttpServletRequest request = ServletActionContext.getRequest();
		
		// courseBean
		CourseBean courseBean = new CourseBean();
		courseBean.setCourseId(String.valueOf(this.getCourseId()));
		// pageInfoBean
		PageInfoBean pageInfoBean = new PageInfoBean();
		pageInfoBean.setIntPage(this.getIntPage());
		// courseId
		int courseIdTemp = this.getCourseId();
		// userBean
		UserBean userBean = new UserBean();
		userBean.setRealname(this.getRealname());

		// 处理和设置pageInfoBean,StudentHomeworkListBean
		StudentHomeworkListBean studentHomeworkListBean = new StudentHomeworkListBean();
		studentHomeworkListBean = homeworkService.homeworkQuery_teacher(courseBean, pageInfoBean, userBean);
		request.setAttribute("studentHomeworkListBean", studentHomeworkListBean);
		request.setAttribute("pageInfoBean", pageInfoBean);
		request.setAttribute("courseId", courseIdTemp);
		
		return "Success";
	}
	
	// 作业查询_学生(根据realname和courseId查找)
	public String homeworkQuery_student() throws SQLException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		// courseBean
		CourseBean courseBean = new CourseBean();
		courseBean.setCourseId(String.valueOf(this.getCourseId()));
		// pageInfoBean
		PageInfoBean pageInfoBean = new PageInfoBean();
		pageInfoBean.setIntPage(this.getIntPage());
		// courseId
		int courseIdTemp = this.getCourseId();
		// userBean
		UserBean userBean = new UserBean();
		userBean.setUserId(String.valueOf(session.getAttribute("userId")));
		// homeworkBean
		HomeworkBean homeworkBean = new HomeworkBean();		
		homeworkBean.setBatch(this.getBatch());

		// 处理和设置pageInfoBean,StudentHomeworkListBean
		StudentHomeworkListBean studentHomeworkListBean = new StudentHomeworkListBean();
		studentHomeworkListBean = homeworkService.homeworkQuery_student(courseBean, pageInfoBean, userBean , homeworkBean);
		request.setAttribute("studentHomeworkListBean", studentHomeworkListBean);
		request.setAttribute("pageInfoBean", pageInfoBean);
		request.setAttribute("courseId", courseIdTemp);
		
		return "Success";
	}
}
